package com.tyylab.kestrel.framework.spring.rpc.feign.annoutil;

import net.bytebuddy.description.annotation.AnnotationDescription;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

public class SpringFeignAnnotationUtil {
    public static AnnotationDescription feignClient(String value, String contextId,String url, String path, Boolean decode404, Boolean primary,
                                                    Class<?> fallback, Class<?> fallbackFactory, String[] qualifiers, Class<?>[] configuration ) {
        return AnnotationDescription.Builder.ofType(FeignClient.class)
                .define("value", StringUtils.hasText(value) ? value : "")
                .define("name", StringUtils.hasText(value) ? value : "")
                .define("contextId", StringUtils.hasText(contextId)? contextId : "")
                .defineArray("qualifiers", ArrayUtils.isEmpty(qualifiers) ? new String[] {} : qualifiers)
                .define("url", StringUtils.hasText(url) ? url : "")
                .define("decode404", null == decode404 ? false : decode404)
                .defineTypeArray("configuration", ArrayUtils.isEmpty(configuration) ? new Class<?>[] {} : configuration )
                .define("fallback", null == fallback ? void.class : fallback)
                .define("fallbackFactory", null == fallbackFactory ? void.class: fallbackFactory)
                .define("path", StringUtils.hasText("path") ? path :"")
                .define("primary", null == primary ? false : primary)
                .build();
    }

    public static AnnotationDescription enableFeign(String[] basePackages,Class<?>[] basePackageClasses, Class<?>[] defaultConfiguration,Class<?>[] clients ){
        return AnnotationDescription.Builder.ofType(EnableFeignClients.class)
                .defineArray("value", !ArrayUtils.isEmpty(basePackages) ? basePackages : new String[]{})
                .defineArray("basePackages", !ArrayUtils.isEmpty(basePackages)? basePackages : new String[]{})
                .defineTypeArray("basePackageClasses", !ArrayUtils.isEmpty(basePackageClasses)? basePackageClasses : new Class<?>[]{})
                .defineTypeArray("defaultConfiguration", !ArrayUtils.isEmpty(defaultConfiguration)?defaultConfiguration : new Class<?>[]{})
                .defineTypeArray("clients", !ArrayUtils.isEmpty(clients)? clients : new Class<?>[]{})
                .build();
    }
}
